//
//  CDConvReportAbuseVC.m
//  LeanChat
//
//  Created by lzw on 15/4/29.
//  Copyright (c) 2015年 LeanCloud. All rights reserved.
//

#import "CDConvReportAbuseVC.h"
#import "CDTextField.h"
#import "CDUserManager.h"
#import "CDUtils.h"

static CGFloat kCDConvReportAbuseVCHorizontalPadding = 10;
static CGFloat kCDConvReportAbuseVCVerticalPadding = 10;
static CGFloat kCDConvReportAbuseVCInputTextFieldHeight = 100;

@interface CDConvReportAbuseVC ()

@property (nonatomic, strong) CDTextField *inputTextField;

@property (nonatomic, strong) NSString *convid;

@end

@implementation CDConvReportAbuseVC


- (instancetype)initWithConversationId:(NSString *)convid;
{
    self = [super init];
    if (self) {
        _convid = convid;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"举报";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(submit:)];
    [self.view addSubview:self.inputTextField];
    UIView * headeView = [[UIView alloc] init];
    headeView.backgroundColor = HEAD_COLOR;
    [self.view addSubview:headeView];
    [headeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(20);
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.height.offset(44);
    }];
 
   
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,80, 45)];
    [backButton setTitle:@"<返回" forState:UIControlStateNormal];
    [backButton setTitleColor:FONT_COLOR forState:UIControlStateNormal];
    
    //    [backButton setImage:[UIImage imageNamed:@"back1.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
    backButton.center = CGPointMake( 120, 32);
    //    backButton.backgroundColor = [UIColor redColor];
    [self.view addSubview:backButton];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(BUTTON_HEIGHT);
        make.left.equalTo(self.view).with.offset(10);
        make.width.offset(50);
        make.height.offset(30);
    }];
    
    
    UIButton *addFriend = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,80, 45)];
    [addFriend setTitle:@"提交" forState:UIControlStateNormal];
    [addFriend addTarget:self action:@selector(submit:) forControlEvents:UIControlEventTouchUpInside];
    [addFriend setTitleColor:FONT_COLOR forState:UIControlStateNormal];
    addFriend.center = CGPointMake( SCREEN_WIDTH - 80, 32);
    //    addFriend.backgroundColor = [UIColor redColor];
    [self.view addSubview:addFriend];
    [addFriend mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(BUTTON_HEIGHT);
        make.right.equalTo(self.view).with.offset(-10);
        make.width.offset(100);
        make.height.offset(30);
    }];
    
    UILabel *titleLbael = [[UILabel alloc] init];
    titleLbael.text = @"举报";
    titleLbael.textAlignment = NSTextAlignmentCenter;
    //    titleLbael.backgroundColor = [UIColor orangeColor];
    titleLbael.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:titleLbael];
    [titleLbael mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(20);
        make.right.equalTo(self.view).with.offset(-100);
        make.left.equalTo(self.view).with.offset(100);
        make.height.offset(30);
    }];

}

- (void)backButton:sender{
    [self dismissViewControllerAnimated:YES completion:nil];

}

- (CDTextField *)inputTextField {
    if (_inputTextField == nil) {
        _inputTextField = [[CDTextField alloc] initWithFrame:CGRectMake(kCDConvReportAbuseVCHorizontalPadding, SCREEN_HEIGHT * 0.3, CGRectGetWidth(self.view.frame) - 2 * kCDConvReportAbuseVCHorizontalPadding, kCDConvReportAbuseVCInputTextFieldHeight)];
        _inputTextField.borderStyle = UITextBorderStyleRoundedRect;
        _inputTextField.horizontalPadding = kCDTextFieldCommonHorizontalPadding;
        _inputTextField.verticalPadding = kCDTextFieldCommonVerticalPadding;
        _inputTextField.placeholder = @"请输入举报原因";
    }
    return _inputTextField;
}

- (void)submit:(id)sender {
    if (self.inputTextField.text.length > 0) {
        WEAKSELF
        DLog(@"%@", self.inputTextField.text);
        [self showProgress];
        [[CDUserManager manager] reportAbuseWithReason:self.inputTextField.text convid:self.convid block: ^(BOOL succeeded, NSError *error) {
            [weakSelf hideProgress];
            if ([self filterError:error]) {
                [self alert:@"感谢您的举报，我们将尽快处理。"];
//                [weakSelf.navigationController popViewControllerAnimated:YES];
                [weakSelf.view removeFromSuperview];
                [weakSelf removeFromParentViewController];
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
